package com.czh.intelligentoj.model.vo;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.*;
import com.czh.intelligentoj.model.dto.question.JudgeConfig;
import com.czh.intelligentoj.model.entity.Question;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;

/*
 * ClassName: Question
 * Package: com.czh.intelligentoj.model.entity
 * Description:
 *             对数据脱敏操作
 * @Author: CZH
 * @Create: 2024
 */
@Data
public class QuestionVO {
    /**
     * id
     */
    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * 标题
     */
    private String title;

    /**
     * 内容
     */
    private String content;

    /**
     * 标签列表 json
     */
    private List<String> tags;

    /**
     * 提交数
     */
    @TableField("submit_num")
    private Integer submitNum;
    /**
     * 提交通过数
     */
    @TableField("accepted_num")
    private Integer acceptedNum;
    /**
     * 用例配置
     */
    @TableField("judge_config")
    private JudgeConfig judgeConfig;
    /**
     * 点赞数
     */
    @TableField("thumb_num")
    private Integer thumbNum;

    /**
     * 收藏数
     */
    @TableField("favour_num")
    private Integer favourNum;

    /**
     * 创建用户 id
     */
    @TableField("user_id")
    private Long userId;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;
    /**
     * 题目创建人的信息
     */
    private UserVO userVO;

    private static final long serialVersionUID = 1L;


    /**
     * 包装类转对象
     *
     * @param questionVO
     * @return
     *       JSON字符串
     */
    public static Question voToObj(QuestionVO questionVO) {
        if (questionVO == null) {
            return null;
        }
        Question question = new Question();
        BeanUtils.copyProperties(questionVO, question);
        List<String> tagList = questionVO.getTags();
        if (tagList != null) {
            question.setTags(JSONUtil.toJsonStr(tagList));
        }
        JudgeConfig judgeConfig = questionVO.getJudgeConfig();
        if (judgeConfig != null) {
            question.setJudgeConfig(JSONUtil.toJsonStr(judgeConfig));
        }
        return question;
    }

    /**
     * 对象转包装类
     *
     * @param question
     * @return
     *       questionVO对象
     */
    public static QuestionVO objToVo(Question question) {
        if (question == null) {
            return null;
        }
        QuestionVO questionVO = new QuestionVO();
        BeanUtils.copyProperties(question, questionVO);
        List<String> tagList = JSONUtil.toList(question.getTags(), String.class);
        questionVO.setTags(tagList);
        String judgeConfigStr = question.getJudgeConfig();
        questionVO.setJudgeConfig(JSONUtil.toBean(judgeConfigStr, JudgeConfig.class));
        return questionVO;
    }
}
